﻿/***
* 定义所有UI窗体的父类。
* 1：Display 显示状态。
* 2：Hiding 隐藏状态
* 3：ReDisplay 再显示状态。
* 4：Freeze 冻结状态。
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel.Design;
using UnityEngine;

namespace UGUIFW
{
    public class BaseUI
    {
        private UIInfo info;
        private Action externalAction;
        /// <summary>
        /// 外部的方法
        /// </summary>
        public Action ExternalAction { get => externalAction; set => externalAction = value; }

        private object externalData;
        /// <summary>
        /// 外部的数据
        /// </summary>
        public object ExternalData { get => externalData; set => externalData = value; }

        //当前UI窗体的基本信息
        public UIInfo Info
        {
            get { return info; }
        }


        public GameObject gameObject;
        public Transform transform;

        public BaseUI(UIInfo ui_info)
        {
            this.info = ui_info;
        }

        //把脚本与UI实体绑定
        public void SetGameObject(GameObject go)
        {
            if (go == null)
            {
                Debug.LogError("初始化时go == null");
                return;
            }
            gameObject = go;
            transform = go.transform;
            Debug.Log("SetGameObject() -- " + gameObject.name);
        }

        #region  窗体的生命周期

        /// <summary>
        /// 显示状态
        /// </summary>
	    public virtual void Display()
        {
            Debug.Log("Display() -- " + gameObject.name);

            this.gameObject.SetActive(true);
            //设置模态窗体调用(必须是弹出窗体)
            if (info.UI_BaseType == EUIBaseType.Popup)
            {
                UIMaskMgr.GetInstance().SetMaskWindow(this.gameObject, info.UI_LucencyType);
            }
        }

        /// <summary>
        /// 隐藏状态
        /// </summary>
	    public virtual void Hide()
        {
            Debug.Log("Hide() -- " + gameObject.name);

            this.gameObject.SetActive(false);
            //取消模态窗体调用
            if (info.UI_BaseType == EUIBaseType.Popup)
            {
                UIMaskMgr.GetInstance().CancelMaskWindow();
            }
        }

        /// <summary>
        /// 重新显示状态
        /// </summary>
	    public virtual void Redisplay()
        {
            Debug.Log("Redisplay() -- " + gameObject.name);

            this.gameObject.SetActive(true);
            //设置模态窗体调用(必须是弹出窗体)
            if (info.UI_BaseType == EUIBaseType.Popup)
            {
                UIMaskMgr.GetInstance().SetMaskWindow(this.gameObject, info.UI_LucencyType);
            }
        }

        /// <summary>
        /// 冻结状态
        /// </summary>
	    public virtual void Freeze()
        {
            Debug.Log("Freeze -- " + gameObject.name);
        }

        public virtual void UnFreeze()
        {
            Debug.Log("UnFreeze -- " + gameObject.name);
        }

        /// <summary>
        /// 每帧更新
        /// </summary>
        public virtual void Update()
        {
        }

        /// <summary>
        /// 销毁
        /// </summary>
        public virtual void DestroyUI()
        {
            Debug.Log("DestroyUI -- " + gameObject.name);

            GameObject.Destroy(gameObject);
            gameObject = null;
            transform = null;
            info = null;
        }

        #endregion

        #region 封装子类常用的方法

        /// <summary>
        /// 注册按钮事件
        /// </summary>
        /// <param name="buttonName">按钮节点名称</param>
        /// <param name="delHandle">委托：需要注册的方法</param>
	    protected void RigisterButtonObjectEvent(string buttonName, EventTriggerListener.VoidDelegate delHandle)
        {
            GameObject goButton = UnityHelper.FindTheChildNode(this.gameObject, buttonName).gameObject;
            //给按钮注册事件方法
            if (goButton != null)
            {
                EventTriggerListener.Get(goButton).onClick = delHandle;
            }
        }

        /// <summary>
        /// 打开UI窗体
        /// </summary>
        /// <param name="uiName"></param>
	    protected void ShowUI(EUIName uIName, bool isAsync = false, Action action = null, object data = null)
        {
            UIManager.GetInstance().ShowUI(uIName, isAsync, action, data);
        }

        /// <summary>
        /// 关闭当前UI窗体
        /// </summary>
	    protected void CloseUI()
        {
            string UIName = string.Empty;            //处理后的UIFrom 名称
            int intPosition = -1;

            UIName = GetType().ToString();             //命名空间+类名
            intPosition = UIName.IndexOf('.');
            if (intPosition != -1)
            {
                //剪切字符串中“.”之间的部分
                UIName = UIName.Substring(intPosition + 1);
            }

            UIManager.GetInstance().CloseUI(this);
        }

        #endregion
    }
}